/**
 * @author yuliang
 *
 */
package com.xuhai.libms.daoImpl;

import java.sql.Date;
import java.util.Collection;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.xuhai.libms.dao.BorrowedBookDao;
import com.xuhai.libms.entity.Book;
import com.xuhai.libms.entity.BorrowedBook;
import com.xuhai.libms.entity.User;


public class BorrowedBookDaoImpl extends HibernateDaoSupport implements BorrowedBookDao {

	public void borrowBook(BorrowedBook borrowedBook) {
		int lib_card = borrowedBook.getLib_card();
		String no = borrowedBook.getBookNo();
		String sql = "from User where lib_card=:lib_card";
		String sql1 = "from Book where no=:no";
		User user = (User) super.getHibernateTemplate().findByNamedParam(sql, "lib_card", lib_card).get(0);
		user.setBcount(user.getBcount()+1);
		super.getHibernateTemplate().update(user);
		
		Book book = (Book)super.getHibernateTemplate().findByNamedParam(sql1, "no", no).get(0);
		book.setBorrowedMarker(1);
		super.getHibernateTemplate().update(book);
		borrowedBook.setMark(1);
		super.getHibernateTemplate().save(borrowedBook);
	}

	public void remove(int id) {
		// TODO Auto-generated method stub

	}

	public void returnBook(BorrowedBook borrowedBook){
		try {
			super.getHibernateTemplate().update(borrowedBook);
			String no = borrowedBook.getBookNo();
			String sql1 = "from Book where no=:no";
			Book book = (Book)super.getHibernateTemplate().findByNamedParam(sql1, "no", no).get(0);
			book.setBorrowedMarker(RETURN_BOOK);
			super.getHibernateTemplate().update(book);
			int lib_card = borrowedBook.getLib_card();
			String sql2 = "from User where lib_card=:lib_card";
			User user = (User)super.getHibernateTemplate().findByNamedParam(sql2, "lib_card", lib_card).get(0);
			user.setBcount((user.getBcount()-1));
			super.getHibernateTemplate().update(user);
		} catch (DataAccessException e) {
			e.printStackTrace();
		}

	}

	public Collection<BorrowedBook> showBook(BorrowedBook borrowedBook) {
		// TODO Auto-generated method stub
		return null;
	}

	public Collection<BorrowedBook> showBookByTime(Date borrowedTime) {
		// TODO Auto-generated method stub
		return null;
	}

	public Collection<BorrowedBook> showBorrowed(int marker, Date borrowedTime) {
		// TODO Auto-generated method stub
		return null;
	}

	public Collection<BorrowedBook> showReturn(int marker, Date returnTime) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Object> showByPage(int start, int max) {
		// TODO Auto-generated method stub
		return null;
	}

	public BorrowedBook returnBookshow(String bookNo) {
		System.out.println(bookNo);
		String sql = "from BorrowedBook bb where bb.bookNo=:bookNo and bb.mark=1";
		 List<BorrowedBook> list = super.getHibernateTemplate().findByNamedParam(sql, "bookNo", bookNo);
		 if(list.size()!=0){
			 return (BorrowedBook) list.get(0);
		 }
		 return null;
	}

}
